package com.aloudmeta.grpc.service;

import com.aloudmeta.grpc.demo2.DubboProto2EchoServiceGrpc;
import com.aloudmeta.grpc.demo2.EchoRequest;
import com.aloudmeta.grpc.demo2.EchoResponse;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * huaixin 2022/1/21 2:34 PM
 */
@DubboService(version = "1.0.0")
@Slf4j
public class Proto2DubboEchoService extends DubboProto2EchoServiceGrpc.Proto2EchoServiceImplBase
    implements DubboProto2EchoServiceGrpc.IProto2EchoService {

    @Override
    public void echo(EchoRequest request, StreamObserver<EchoResponse> responseObserver) {
        String message = request.getMessage();
        String reply = "echo: " + message;
        log.info(reply);

        SecurityContext securityContext = SecurityContextHolder.getContext();

        //throw new IllegalArgumentException("参数不正确");
        EchoResponse response = EchoResponse.newBuilder()
                .setMessage(reply)
                .build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }
}